<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <!-- -->
          <el-button type="primary" v-on:click="serch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleBuilding">添加楼栋</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="handleUnit">添加单元</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="handleAdd">添加房屋</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="house" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="30">
      </el-table-column>
      <el-table-column prop="number" label="房屋编号" width="100" sortable>
      </el-table-column>
      <el-table-column prop="buildingArea" label="建筑面积" width="100" sortable>
      </el-table-column>
      <el-table-column prop="decorationType" label="精装修" width="100" sortable>
        <template scope="scope">
          <span style="color: #13ce66" v-if="scope.row.decorationType ==1">精装修</span>
          <span style="color: #e64242" v-else-if="scope.row.decorationType ==0">简装修</span>
          <span style="color: #c0ccda" v-else>未装修</span>
        </template>
      </el-table-column>
      <el-table-column prop="floor" label="楼层" width="80" sortable>
      </el-table-column>
      <el-table-column prop="housingType" label="房屋类型" width="120" sortable>
      </el-table-column>
      <!-- :formatter="formatState" -->
      <el-table-column prop="indoorArea" label="室内面积" width="100" sortable>
      </el-table-column>
      <el-table-column prop="remarks" label="房屋描述" min-width="140" sortable>
      </el-table-column>
      <el-table-column prop="owner.name" label="业主信息" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="unit.unitNumber" label="单元编号" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="building.name" label="楼栋信息" min-width="100" sortable>
      </el-table-column>
      <el-table-column label="操作" width="290">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--房屋页面编辑和添加-->
    <el-dialog title="房屋编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="房屋编号" prop="number">
          <el-input v-model="editForm.number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="editForm.floor" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋类型" prop="housingType">
          <el-input v-model="editForm.housingType" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋面积" prop="buildingArea">
          <el-input v-model="editForm.buildingArea" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="建筑面积" prop="indoorArea">
          <el-input v-model="editForm.indoorArea" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="精装修">
          <el-radio-group v-model="editForm.decorationType">
            <el-radio class="radio" :label="1">精装修</el-radio>
            <el-radio class="radio" :label="0">简装修</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="楼栋信息">
          <el-select v-model="editForm.buildingId" placeholder="请选择"
                     @change="changBuildings(editForm.buildingId)">
            <el-option
                v-for="item in buildings"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元信息">
          <el-select v-model="editForm.unitId" placeholder="请选择">
            <el-option
                v-for="item in units"
                :label="item.unitNumber"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

<!--        <el-form-item label="业主信息">-->
<!--          <el-select v-model="editForm.ownerId" placeholder="请选择">-->
<!--            <el-option-->
<!--                v-for="item in owners"-->
<!--                :label="item.name"-->
<!--                :value="item.id">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="描述" prop="remarks">
          <el-input v-model="editForm.remarks" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--添加楼栋-->
    <el-dialog title="楼栋添加" :visible.sync="editBuilding" :close-on-click-modal="false">
      <el-form :model="editBuild" label-width="80px" ref="editForm">
        <el-form-item label="楼栋编号" prop="number">
          <el-input v-model="editBuild.number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼栋名称" prop="name">
          <el-input v-model="editBuild.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼栋面积" prop="floorArea">
          <el-input v-model="editBuild.floorArea" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋面积" prop="buildingArea">
          <el-input v-model="editBuild.buildingArea" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="editBuild.remarks" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editBuilding = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmitBuild" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--添加单元-->
    <el-dialog title="单元添加" :visible.sync="editUnit" :close-on-click-modal="false">
      <el-form :model="editUnits" label-width="80px" ref="editForm">
        <el-form-item label="单元编号" prop="unitNumber">
          <el-input v-model="editUnits.unitNumber" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="总层数" prop="unitLayers">
          <el-input v-model="editUnits.unitLayers" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="建筑面积" prop="floorArea">
          <el-input v-model="editUnits.floorArea" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否电梯" prop="elevatorType">
          <el-radio-group v-model="editUnits.elevatorType">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="楼栋信息">
          <el-select v-model="editUnits.buidingId" placeholder="请选择">
            <el-option
                v-for="item in buildings"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="editUnits.remarks" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editUnitsing = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmitUnit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>

export default {
  data() {
    return {
      table: false,
      options: [],
      units: [],
      buildings: [],
      owners: [],
      // 四大小金刚
      totals: 0,
      pageSize: 5,
      currentPage: 1,
      filters: {
        keyword: '',
        name: ''
      },
      // 获取后台数据
      house: [],
      listLoading: false,
      sels: [],//列表选中列
      // 添加楼栋模态框
      editBuilding: false,
      editUnit: false,
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ],
      },
      //编辑界面数据
      editForm: {
        number: null,
        floor: null,
        housingType: null,
        buildingArea: null,
        indoorArea: null,
        decorationType: null,
        unitId: null,
        remarks: null,
        buildingId: null,
        ownerId: null,
      },
      // 楼栋添加
      editBuild: {
        number: null,
        name: null,
        floorArea: null,
        remarks: ""
      },
      // 单元添加
      editUnits: {
        unitNumber: null,
        unitLayers: null,
        floorArea: null,
        elevatorType: null,
        buidingId: null,
        remarks: null
      },
      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //新增界面数据
      addForm: {
        number: null,
        floor: null,
        housingType: null,
        buildingArea: null,
        indoorArea: null,
        decorationType: null,
        unitId: null,
        remarks: null,
        buildingId: null,
        ownerId: null,
      }

    }
  },
  methods: {
    changBuildings(id){
      this.$http.get("/unit/change/"+ id).then(result=>{
        this.units = result.data
      })
      this.editForm.unitId = null

    },
    // 添加单元的模态框
    handleUnit() {
      this.editUnit = true
    },

    // 添加楼栋模态框
    handleBuilding() {
      this.editBuilding = true
    },
    serch() {
      this.currentPage = 1
      this.getHousing();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getHousing();
    },
    //获取用户列表
    getHousing() {
      let para = {
        currentPage: this.currentPage,
        keyword: this.filters.keyword,
        pageSize: this.pageSize,
      };
      // 忙等
      this.listLoading = true;
      // 发送请求：获取后台数据
      this.$http.post("/house/list", para)
          .then(result => {
            this.house = result.data.rows;
            this.totals = result.data.totals;
            this.listLoading = false;
          })
          .catch(result => {
            this.$message({
              message: '网络不通',
              type: 'error'
            });
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送请求
        this.$http.delete("/house/" + row.id)
            .then(result => {
              this.listLoading = false;
              if (result.data.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                })
              }
              this.currentPage = 1,
                  this.getHousing();
            })
            .catch(result => {
              this.$message({
                    message: '网络异常',
                    type: 'error'
                  },
              );
            })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.editFormVisible = true;
      this.editForm = {
        number: null,
        floor: null,
        housingType: null,
        buildingArea: null,
        indoorArea: null,
        decorationType: null,
        unitId: null,
        remarks: null,
        buildingId: null,
        ownerId: null,
      };
    },
    // 房屋编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.editForm);
            // 发送请求
            this.$http.put("/house", para)
                .then(result => {
                  this.listLoading = false;
                  if (result.data.success) {
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                    this.editLoading = false;
                  } else {
                    this.editLoading = false;
                    this.$message({
                      message: result.data.msg,
                      type: 'error',

                    })
                  }
                  this.addLoading = false;
                  this.editLoading = false;
                  this.currentPage = 1,
                      this.getHousing();
                  this.editFormVisible = false;

                })
                .catch(result => {
                  this.$message({
                        message: '网络异常',
                        type: 'error'
                      },
                  );
                })

          });
        }
      });
    },

    // 添加楼栋信息
    editSubmitBuild: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.editBuild);
            console.log(para)
            // 发送请求
            this.$http.put("/building", para)
                .then(result => {
                  this.listLoading = false;
                  if (result.data.success) {
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                    this.editLoading = false;
                  } else {
                    this.$message({
                      message: '操作失败',
                      type: 'error'
                    })
                  }
                  this.currentPage = 1,
                      this.getHousing();
                  this.editBuilding = false;
                  this.editBuild = {
                    number: null,
                    name: null,
                    floorArea: null,
                    remarks: ""
                  };
                })
                .catch(result => {
                  this.$message({
                        message: '网络异常',
                        type: 'error'
                      },
                  );
                })

          });
        }
      });
    },

    // 添加单元
    editSubmitUnit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.editUnits);
            // 发送请求
            this.$http.put("/unit", para)
                .then(result => {
                  this.listLoading = false;
                  if (result.data.success) {
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                    this.editLoading = false;
                  } else {
                    this.editLoading = false;
                    this.$message({
                      message: result.data.msg,
                      type: 'error'
                    })
                  }
                  this.addLoading = false;
                  this.currentPage = 1,
                      this.getHousing();
                  this.editUnit = false;
                  this.editUnits={
                    unitNumber: null,
                        unitLayers: null,
                        floorArea: null,
                        elevatorType: null,
                        buidingId: null,
                        remarks: null
                  };
                })
                .catch(result => {
                  this.$message({
                        message: '网络异常',
                        type: 'error'
                      },
                  );
                })

          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/house", ids)
            .then(result => {
              this.listLoading = false;
              if (result.data.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                })
              }
              this.currentPage = 1,
                  this.getHousing();
            })
            .catch(result => {
              this.$message({
                    message: '网络异常',
                    type: 'error'
                  },
              );
            })
      }).catch(() => {

      });
    },

    // 单元下拉框
    getUnits() {
      this.$http.get("/unit").then(result => {
        this.units = result.data
      })
    },
    // 楼栋下拉框
    getBuildings() {
      this.$http.get("/building").then(result => {
        this.buildings = result.data
      })
    },
    getOwners() {
      this.$http.get("/owner").then(result => {
        this.owners = result.data
      })
    },
  },
  mounted() {
    this.getHousing();
    this.getUnits();
    this.getBuildings();
    this.getOwners();
  }
}

</script>

<style scoped>

</style>